###############################################################################
############# Script for replication - ONLINE APPENDIX  ####################
###############################################################################
# R version 4.3.1
# (!) run main script(Script_APSR_main.R) before running the manuscript for reproducing the online appendix

######### APPENDIX C: Full tables of the results upon the figures in the manuscript are based  #########
## Reproduce: 
##      - Table C.1 (Appendix)  
##
## Output: 
##      Table C. 1: Effects of minipublic attributes on support across countries 
##				 

head(amce_choice[c("level", "estimate", "std.error")], 19L)
summary(amce_choice)

## Reproduce: 
##      - Table C.2 (Appendix)  
##
## Output: 
##      Table C. 2: Differences between countries 
##

head(amce_by[c("level", "estimate", "std.error")], 57L)

## Reproduce: 
##      - Table C.3 (Appendix)  
##    
## Output: 
##      Table C.3: Effects of minipublic attributes on support conditional on familiarity and experiences 
##       

head(diff_experience_choice2[c("level", "estimate", "std.error")], 30L)

## Reproduce: 
##      - Table C.4 (Appendix)  
##      
## Output: 
##      Table C.4: Effects of minipublic attributes on support conditional on low political trust and high trust in citizens 
##      

head(diff_disent_trust1[c("level", "estimate", "std.error")], 40L)
head(diff_disent_trust2[c("level", "estimate", "std.error")], 40L)

## Reproduce: 
##      - Table C.5 (Appendix)  
##      
## Output: 
##      Table C.5: Combined effect for low political trust but high trust in citizens 
##      

head(diff_disent_select[c("level", "estimate", "std.error")], 40L)


######### APPENDIX B: Population, Measurements and descriptive information #########

##### Descriptive information on sample vs population #####
## Reproduce: 
##      - Table B.1 (Appendix)
##      - Table B.2 (Appendix) - see methods report Psyma
##      - Table B.3 (Appendix) - see methods report Psyma
## Output: 
##      Table B.1: Description of the sample and population in Finland
##
setwd("..")
getwd ()

# data sets per country
Fin <- read_sav ("socio_Fin.sav")

## sex
class(Fin$S2_gender)
Fin$gender <- factor(Fin$S2_gender, labels = c("Male", "Female", "Other", "dont want to say"))
summary(Fin$gender)
Fin %>% group_by(gender) %>% dplyr::summarise(Percentage=n()/nrow(.))

## age
class(Fin$S1_age)
Fin$age <- as.numeric(Fin$S1_age)
describe(Fin$age)
# select cases with valid values
Fin_age <- Fin |>
  filter(age >= 18 & age <= 150)
describe (Fin_age$age)
# compute age groups
Fin_age <- Fin_age %>%
  mutate(
    age_group = case_when(
      age >= 18  & age <= 39 ~ "18-39",
      age >= 40  & age <= 59 ~ "40-59",
      age >= 60              ~ "60+")
  )
class(Fin_age$age_group) 
Fin_age$age_group <- as.factor(Fin_age$age_group)
summary(Fin_age$age_group)
Fin_age %>% group_by(age_group) %>% dplyr::summarise(Percentage=n()/nrow(.))

## education
attributes(Fin$S4_education)
class(Fin$S4_education)
describe(Fin$S4_education)
# select cases with valid values
Fin_edu <- Fin |>
  filter(S4_education >= 1 & S4_education <= 9)
describe (Fin_edu$S4_education)

Fin_edu <- Fin_edu %>%
  mutate(
    edu_group = case_when(
      S4_education == 1 ~ "Below secondary II (ISCED 0-3)",
      S4_education == 2 ~ "Below secondary II (ISCED 0-3)",
      S4_education == 3 ~ "Secondary II (ISCED 3-4)",
      S4_education == 4 ~ "Secondary II (ISCED 3-4)", 
      S4_education == 5 ~ "Secondary II (ISCED 3-4)",
      S4_education == 6 ~ "Tertiary (ISCED 5-8) ",
      S4_education == 7 ~ "Tertiary (ISCED 5-8) ",
      S4_education == 8 ~ "Tertiary (ISCED 5-8) ",
      S4_education == 9 ~ "Below secondary II (ISCED 0-3)")
  )
class(Fin_edu$edu_group)
Fin_edu$edu_group <- as.factor(Fin_edu$edu_group)
summary(Fin_edu$edu_group)
Fin_edu %>% group_by(edu_group) %>% dplyr::summarise(Percentage=n()/nrow(.))


##### Descriptive information for retrospective assessment of minipublics  #####
## Reproduce: 
##      - Table B.4 (Appendix)  
##
## Output: 
##      Table B.4: Descriptive information on support for minipublics 
##

describe(cj_trust$retro) # mean 4.85 median 5
describe(cj_fin$retro) # mean 4.47 median 4
describe(cj_ire$retro) # mean 5.05 median 5
describe(cj_usa$retro) # mean 5.04 median 5

##### Descriptive information for familiarity and experience #####
## Reproduce: 
##      - Table B.5 (Appendix)  
##
## Output: 
##      Table B.5: Share of respondents who are (un)familiar with minipublics 
##

# All countries
cj_experience %>% group_by(experiences) %>% dplyr::summarise(Percentage=n()/nrow(.))
# FIN
experiences_fin <- cj_experience %>% filter(country == "FIN")
experiences_fin %>% group_by(experiences) %>% dplyr::summarise(Percentage=n()/nrow(.))
# IRE
experiences_ire <- cj_experience %>% filter(country == "IRE")
experiences_ire %>% group_by(experiences) %>% dplyr::summarise(Percentage=n()/nrow(.))
# USA
experiences_usa <- cj_experience %>% filter(country == "US")
experiences_usa %>% group_by(experiences) %>% dplyr::summarise(Percentage=n()/nrow(.))
# correlations
cj_experience$participated_num <- as.numeric(cj_experience$participated)
cj_experience %>% group_by(participated_num) %>% dplyr::summarise(Percentage=n()/nrow(.))
cj_experience %>% group_by(experiences_direct) %>% dplyr::summarise(Percentage=n()/nrow(.))

cor(cj_experience$participated_num, cj_experience$experiences_direct,  method = "kendall", use = "complete.obs")
cor(cj_experience$participated_num, cj_experience$experiences_direct,  method = "spearman", use = "complete.obs")

library(gmodels)

CrossTable(cj_experience$participated_num, cj_experience$experiences_direct,  
           prop.c = TRUE, prop.r = FALSE, prop.t = FALSE, prop.chisq = FALSE,
           expected = FALSE,
           dnn = c( "Participated","Direction of experience"))

# only those who are at least familiar with minipublics
cj_expdirect$participated <- ifelse(cj_expdirect$experiences == 4, 1, 0)
cj_expdirect %>% group_by(participated) %>% dplyr::summarise(Percentage=n()/nrow(.))
cj_expdirect %>% group_by(experiences_direct) %>% dplyr::summarise(Percentage=n()/nrow(.))

cor(cj_expdirect$participated, cj_expdirect$experiences_direct,  method = "kendall", use = "complete.obs")
cor(cj_expdirect$participated, cj_expdirect$experiences_direct,  method = "spearman", use = "complete.obs")

CrossTable(cj_expdirect$participated, cj_expdirect$experiences_direct,  
           prop.c = TRUE, prop.r = FALSE, prop.t = FALSE, prop.chisq = FALSE,
           expected = FALSE,
           dnn = c( "Participated","Direction of experience"))

##### Descriptive information for TRUST variables #####
## Reproduce: 
##      - Table B.6 (Appendix)
##      - Table B.7 (Appendix) 
##      - Table B.8 (Appendix) 
##      - Table B.9 (Appendix)
## Output: 
##      Table B.6: Share of respondents who (dis)trust political institutions (index)
##      Table B.7: Share of respondents who (dis)trust politicians
##      Table B.8: Share of respondents who (dis)trust citizens (index)
##      Table B.9: Share of respondents who (dis)trust minipublics

# All countries
d1.summary.extended <- cj_trust %>%
  select(trust_politics, trust_politicians, trust_dmp, trust_citizens) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()
tmp1 <- cj_trust[, c("trust_politics", "trust_politicians",
                     "trust_dmp", "trust_citizens")]
tmp_list1 <- lapply(tmp1, na.omit)
tmp_list1 <- lapply(tmp_list1, scale)
emptycol = function(x) " "
modelsummary::datasummary(trust_politics + trust_politicians + trust_dmp + trust_citizens ~ N + Min + Max + Mean + Median + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp1) %>%
  column_spec(column = 8, image = spec_boxplot(tmp_list1)) %>%
  column_spec(column = 9, image = spec_hist(tmp_list1))

# FIN
trust_fin <- cj_trust %>% filter(country == "FIN")
tmp2 <- trust_fin[, c("trust_politics", "trust_politicians",
                      "trust_dmp", "trust_citizens")]
tmp_list2 <- lapply(tmp2, na.omit)
tmp_list2 <- lapply(tmp_list2, scale)
emptycol = function(x) " "
datasummary(trust_politics + trust_politicians + trust_dmp + trust_citizens ~ N + Min + Max + Mean + Median + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp2) %>%
  column_spec(column = 8, image = spec_boxplot(tmp_list2)) %>%
  column_spec(column = 9, image = spec_hist(tmp_list2))

# IRE
trust_ire <- cj_trust %>% filter(country == "IRE")
tmp3 <- trust_ire[, c("trust_politics", "trust_politicians",
                      "trust_dmp", "trust_citizens")]
tmp_list3 <- lapply(tmp3, na.omit)
tmp_list3 <- lapply(tmp_list3, scale)
emptycol = function(x) " "
datasummary(trust_politics + trust_politicians + trust_dmp + trust_citizens ~ N + Min + Max + Mean + Median + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp3) %>%
  column_spec(column = 8, image = spec_boxplot(tmp_list2)) %>%
  column_spec(column = 9, image = spec_hist(tmp_list2))

# USA
trust_usa <- cj_trust %>% filter(country == "US")
tmp4 <- trust_usa[, c("trust_politics", "trust_politicians",
                      "trust_dmp", "trust_citizens")]
tmp_list4 <- lapply(tmp4, na.omit)
tmp_list4 <- lapply(tmp_list4, scale)

emptycol = function(x) " "
datasummary(trust_politics + trust_politicians + trust_dmp + trust_citizens ~ N + Min + Max + Mean + Median + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp4) %>%
  column_spec(column = 8, image = spec_boxplot(tmp_list2)) %>%
  column_spec(column = 9, image = spec_hist(tmp_list2))

# Table B. 6: Share of respondents who (dis)trust political institutions (index)
# All countries: 36% distrust, 22% extremely distrust (1 and 2 at a 7pt scale), 44% trust
prop.table(table(cj_trust$trust_politics))
# FIN: 29% distrust, 16% extremely distrust (1 and 2 at a 7pt scale), 53% trust
prop.table(table(trust_fin$trust_politics))
# IRE: 31% distrust, 18% extremely distrust (1 and 2 at a 7pt scale), 46% trust
prop.table(table(trust_ire$trust_politics))
# USA: 50% distrust, 34% extremely distrust (1 and 2 at a 7pt scale), 30% trust
prop.table(table(trust_usa$trust_politics))

# Table B. 7: Share of respondents who (dis)trust politicians
# All countries: 53% distrust, 35% extremely distrust (1 and 2 at a 7pt scale), 44% trust
prop.table(table(cj_trust$trust_politicians))
# FIN: 43% distrust, 24% extremely distrust (1 and 2 at a 7pt scale), 43% trust
prop.table(table(trust_fin$trust_politicians))
# IRE: 50% distrust, 31% extremely distrust (1 and 2 at a 7pt scale), 33% trust
prop.table(table(trust_ire$trust_politicians))
# USA: 66% distrust, 50% extremely distrust (1 and 2 at a 7pt scale), 20% trust
prop.table(table(trust_usa$trust_politicians))

# Table B. 8:  Share of respondents who (dis)trust citizens (index)
# All countries: 17% distrust, 7% extremely distrust (1 and 2 at a 7pt scale), 55% trust
prop.table(table(cj_trust$trust_citizens))
# FIN: 19% distrust, 7% extremely distrust (1 and 2 at a 7pt scale), 51% trust
prop.table(table(trust_fin$trust_citizens))
# IRE: 13% distrust, 4% extremely distrust (1 and 2 at a 7pt scale), 60% trust
prop.table(table(trust_ire$trust_citizens))
# USA: 18% distrust, 8% extremely distrust (1 and 2 at a 7pt scale), 53% trust
prop.table(table(trust_usa$trust_citizens))

# Table B. 9: Share of respondents who (dis)trust minipublics
# All countries: 17% distrust, 7% extremely distrust (1 and 2 at a 7pt scale), 52% trust
prop.table(table(cj_trust$trust_dmp))
# FIN: 23% distrust, 9% extremely distrust (1 and 2 at a 7pt scale), 40% trust
prop.table(table(trust_fin$trust_dmp))
# IRE: 12% distrust, 4% extremely distrust (1 and 2 at a 7pt scale), 63% trust
prop.table(table(trust_ire$trust_dmp))
# USA: 17% distrust, 8% extremely distrust (1 and 2 at a 7pt scale), 54% trust
prop.table(table(trust_usa$trust_dmp))

##### Factor analysis trust variables #####
## Reproduce: 
##      - Table B.10 (Appendix)
##       
## Output: 
##      Table B.10: Factor analysis of all trust variables
##      
trust <- cj_trust |> select(trust_parliament, trust_government, trust_politicians, trust_dmp, trust_citizens1, trust_citizens2)

trust %>% 
  select(trust_parliament, trust_government, trust_politicians, trust_dmp, trust_citizens1, trust_citizens2)%>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()

trust_m=trust[complete.cases(trust),]
cor(trust_m)
cortest.bartlett(trust_m)
KMO(trust_m)
nfactors(trust_m, rotate = "varimax", fm="mle") 
ev <- eigen(cor(trust_m)) 
ap <- parallel(subject=nrow(trust_m),var=ncol(trust_m),rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS) 

PCA_trust <- principal(trust_m, nfactors=2, rotate="varimax")
print(PCA_trust)
plot(PCA_trust)
print(loadings(PCA_trust), cutoff= .3)
PCA_trust$values
PCA_trust$n.obs
PCA_trust$communality

# correlations
trust$trust_politics <- rowMeans(subset(trust, select = c(trust_parliament, trust_government)), na.rm = TRUE)
describe(trust$trust_politics)
trust$trust_citizens <- rowMeans(subset(trust, select = c(trust_citizens1, trust_citizens2)), na.rm = TRUE)
describe(trust$trust_citizens)

cor(trust$trust_politics, trust$trust_politicians, method = "pearson", use = "complete.obs")
cor(trust$trust_dmp, trust$trust_citizens, method = "pearson", use = "complete.obs")

trust$trust_pol <- rowMeans(subset(trust, select = c(trust_parliament, trust_government, trust_politicians)), na.rm = TRUE)
describe(trust$trust_pol)
trust$trust_cit <- rowMeans(subset(trust, select = c(trust_citizens1, trust_citizens2, trust_dmp)), na.rm = TRUE)
describe(trust$trust_cit)

cor(trust$trust_pol, trust$trust_cit, method = "pearson", use = "complete.obs")

##### Level of political dissatisfaction across countries #####
## Reproduce: 
##      - Table B.11 (Appendix)
##       
## Output: 
##      Table B. 11: Share of respondents who are (dis)satisfied with democracy
## 
# define missings
d_fin_1 <- cj_fin %>%
  select(swd) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()
cj_fin$swd [cj_fin$swd==8] <- NA

d_ire_1 <- cj_ire %>%
  select(swd) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()
cj_ire$swd [cj_ire$swd==997] <- NA

d_usa_1 <- cj_usa %>%
  select(swd) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()
cj_usa$swd [cj_usa$swd==997] <- NA

# FIN: 25% dissatisfied, 13% extremely dissatisfied (1 and 2 at a 7pt scale), 59% satisfied
cj_fin %>% group_by(swd) %>% summarise(Percentage=n()/nrow(.))
prop.table(table(cj_fin$swd))
# IRE: 25% dissatisfied, 13% extremely dissatisfied, 55% satisfied
prop.table(table(cj_ire$swd))
# USA: 32% dissatisfied, 22% extremely dissatisfied, 50% satisfied
prop.table(table(cj_usa$swd))

##### Level of salience across countries  #####
## Reproduce: 
##      - Table B.12 (Appendix)
##       
## Output: 
##      Table B. 12: Share of respondents who find the issues (un)important
## 
describe(cj_trust$salience_climate) # mean 5  median 5
describe(cj_trust$salience_immigration) # mean 4.95 median 5

cj_fin %>%
  select(salience_climate:salience_immigration) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()
cj_ire %>%
  select(salience_climate:salience_immigration) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()
cj_usa %>%
  select(salience_climate:salience_immigration) %>%
  psych::describe(quant=c(.25,.75)) %>%
  as_tibble(rownames="rowname")  %>%
  print()
#FIN: Climate: mean - 4.75, median - 5; Immigration: mean - 4.78, median - 5
#IRE: Climate: mean - 5.22, median - 6; Immigration: mean - 4.82, median - 5
#USA: Climate: mean - 5.04, median - 6; Immigration: mean - 5.25, median - 6

# Climate: unimportant (<4) 20.3% - important (>4) 64.9%
sal_cli <- table(cj_trust$salience_climate)
f_sal_cli <- prop.table(sal_cli)
f_sal_cli <-round(f_sal_cli, digits = 3)
cbind(sal_cli, f_sal_cli)
# Immigration: unimportant (<4) 17.7% - important (>4) 62.1%
sal_imm <- table(cj_trust$salience_immigration)
f_sal_imm <- prop.table(sal_imm)
f_sal_imm <-round(f_sal_imm, digits = 3)
cbind(sal_imm, f_sal_imm)

